package table.col;

import java.util.List;

import table.Col;
import table.DataTable;
import table.node.BaseNode;
import table.node.DataNode;

public class A2BSumData extends ResultA2BCol {

	private String dstColName;
	public void setDstColName(String dstColName) {
		this.dstColName = dstColName;
	}
	public String getDstColName() {
		return dstColName;
	}

	protected DataNode getColValueFromBTable(DataTable bTable){
		List<Col> bCols = this.getTableColsByNames(this.getBTable(), this.getDstColNames());
		Col dstCol = null;
		if(this.getDstColName() != null){
			dstCol = bTable.getColByName(this.getDstColName());
		}
		DataNode node = new BaseNode();
		if(dstCol == null){
			node.setNodeValue(bCols.get(0).getDataSize());
		} else {
			double sumResult = 0.0;
			for(int i=0; i < dstCol.getDataSize(); i++){
				sumResult += Double.parseDouble(dstCol.getValue(i).toString());
			}
			node.setNodeValue(sumResult);
		}
		return node;
	}
	
	protected DataNode getColValueFromBTable(DataTable bTable, int aRowIndex){
		  List<Col> ACols = this.getTableColsByNames(getATable(), getAColNames());
		  List<Col> BCols = this.getTableColsByNames(getBTable(), getBColNames());
		  List<Col> A_BACols = this.getTableColsByNames(getA2BTable(), getA2BAColNames());
		  List<Col> A_BBCols = this.getTableColsByNames(getA2BTable(), getBColNames());
		  DataNode node = new BaseNode();
			Double sumAll = 0.0;
			Col dstCol = this.getBTable().getColByName(this.getDstColName());
			if(dstCol == null){
				dstCol = this.getTableColsByNames(this.getBTable(), this.getDstColNames()).get(0);
			}
		  if(ACols != null && BCols != null && ACols.size() > 0){
			  if(A_BACols == null || A_BBCols == null){
				  for(int bRowIndex =0; bRowIndex < BCols.get(0).getDataSize(); bRowIndex++){
					  boolean flag = false;
					  for(int colIndex = 0; colIndex < ACols.size(); colIndex++){
						  if(ACols.get(colIndex).getValue(aRowIndex).equals(BCols.get(colIndex).getValue(bRowIndex))){
							  flag = true;
						  } else {
							  flag = false;
							  break;
						  }
					  }
					  if(flag){
						  sumAll += Double.parseDouble(dstCol.getValue(bRowIndex).toString());
					  }
				  }
			  } else {
				  for(int bRowIndex =0; bRowIndex < BCols.get(0).getDataSize(); bRowIndex++){
					  boolean flag = false;
					  for(int a_bRowIndex = 0; a_bRowIndex < A_BBCols.get(0).getDataSize(); a_bRowIndex++){
						  for(int bColIndex = 0; bColIndex < BCols.size(); bColIndex++){
							  if(A_BBCols.get(bColIndex).getValue(a_bRowIndex).equals(BCols.get(bColIndex).getValue(bRowIndex))){
								  flag = true;
							  } else {
								  flag = false;
								  break;
							  }
						  }
						  if(flag == false){continue;}
						  for(int aColIndex = 0; aColIndex < ACols.size(); aColIndex++){
							  if(A_BACols.get(aColIndex).getValue(a_bRowIndex).equals(ACols.get(aColIndex).getValue(aRowIndex))){
								  flag = true;
							  } else {
								  flag = false;
								  break;
							  }
						  }
					  }
					  if(flag){
						  sumAll += Double.parseDouble(dstCol.getValue(bRowIndex).toString());
					  }
				  }
			  }
		  }
		
		node.setNodeValue(sumAll);
		return node;
	}
}
